package com.my.controller.System;


import com.alibaba.fastjson.JSON;
import com.my.bean.Messge;
import com.my.bean.User;
import com.my.service.MessgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 消息通知表 前端控制器
 * </p>
 *
 * @author 张子艺
 * @since 2018-08-29
 */
@Controller
@RequestMapping("/messge")
public class MessgeController {
    @Autowired
    MessgeService messgeService;
    //请求到消息聊天的窗口页面
    @RequestMapping("/main.html")
    public String home(){
        return "/System/messge";

    }

    //点击发送时 保存信息到数据库
    @RequestMapping("/insertMSG")
    @ResponseBody
    public String insertMSG(Messge messge, HttpSession session){
          messge.setCreateTime(new Date().getTime());
         messge.setRoleName(((User)session.getAttribute("user")).getRoles().getRoleName());
        messge.setUsername((((User)session.getAttribute("user")).getUsername()));
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        if(messgeService.insert(messge)){
            //返回出提示消息
            jsonMap.put("flag","1");
            return JSON.toJSONString(jsonMap);
        }else {
            //返回出提示消息
            jsonMap.put("flag","2");
            return JSON.toJSONString(jsonMap);
        }


    }


    //请求到消息聊天的窗口页面
    @RequestMapping("/findMSG")
    @ResponseBody
    public List<Messge> findMSG(){
        return messgeService.findMSG();
    }
}
